/*
 * $Id$
 */
package py.windmill.web.dict;

import com.eclipsesource.json.JsonArray;
import com.eclipsesource.json.JsonObject;
import com.eclipsesource.json.JsonValue;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import java.util.HashMap;
import java.util.Map;
import java.util.zip.GZIPInputStream;

import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;

/**
 *
 * @author myang
 */
public class JsonDictionary implements DictionaryService {
    final static String[] LETTERS = {"a", "b", "c", "d", "e", "f", "g", "h",
                                     "i", "j", "k", "l", "m", "n", "o", "p", 
                                     "q", "r", "s", "t", "u", "v", "w", "x", 
                                     "y", "z"};

    static Map<String, String> _entries = new HashMap<String, String>();

    static JsonObject _dict;
    static {
        try {
            final InputStream is = 
              new GZIPInputStream(JsonDictionary.class.getResourceAsStream("a-z.json.gz"));
            final InputStream entries = 
              new GZIPInputStream(JsonDictionary.class.getResourceAsStream("entries.tgz"));
            try {
                final BufferedReader br = 
                    new BufferedReader(new InputStreamReader(is, "utf-8"));
                _dict = JsonObject.readFrom(br);

                final TarArchiveInputStream tar = new TarArchiveInputStream(entries, "utf-8");
                for (TarArchiveEntry e = 
                  tar.getNextTarEntry(); null != e; e = tar.getNextTarEntry()) {
                    final byte[] content = new byte[(int)e.getSize()];
                    int offset = 0;
                    while (offset < content.length) {
                        final int i = tar.read(content, offset, content.length - offset);
                        offset += i;
                    }
                    _entries.put(e.getName(), new String(content, "utf-8"));
                }

            }
            finally {
                is.close();
                entries.close();
            }
        }
        catch (final Exception ex) {
            ex.printStackTrace();
        }
    }

    @Override
    public Word selectWord(final String word, final int sn) {
        for (final String letter : LETTERS) {
            final JsonValue jv = _dict.get(letter).asObject().get(word);
            if (null != jv) {
                final Word w = new Word(word);
                final JsonArray entries = jv.asArray();
                for (int i = 0; i < entries.size(); ++i) {
                    final String name = entries.get(i).asString();
                    if (i == sn) {
                        w.setSelectedEntryIndex(sn)
                          .add(name, _entries.get("entries/" + name));
                    }
                    else {
                        w.add(name);
                    }
                }
                return w;
            }
        }
        return null;
    }

    @Override
    public String selectEntry(String word, int sn) {
        throw new UnsupportedOperationException("Not supported yet."); 
    }

    /**
     * 
     * @param args 
     */
    public static void main(final String... args) {
        final JsonDictionary jd = new JsonDictionary();
        System.out.println(jd.selectWord("meander", 0));
    }
    
}
